﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRWEB.HtmlHelpers;
using System.Web.Routing;
using System.Data;

namespace HRWEB.Controllers
{
    [Authorize]
    public class InfoSummaryController : Controller
    {
        hrdbEntities ent = new hrdbEntities();
        [HttpGet]      
        public ActionResult Index()
        {
            string id = HttpContext.Profile["ID"].ToString();
            var scale = from su in ent.TInfoSummaries where su.FEmployeeID == id orderby su.SerialNO descending select su;
            if (scale.Any())
            {
                return PartialView(scale.First());
            }
            else
            {
                TInfoSummary sum = ent.TInfoSummaries.Create();
                sum.Feedback = "我确认以上所有信息准确无误";
                return PartialView(sum);
            }
        }
        [HttpPost]
        public ActionResult Index(TInfoSummary s)
        {
            var s_emp = from ee in ent.TEmployees where ee.FPublicID == User.Identity.Name select ee;
            TEmployee emp = s_emp.Single();
            s.FullyFilled = false;
            if (!emp.Validate())
            {
                TempData["Message"] = "信息填写不完整，请仔细核对";
            }
            else if (string.IsNullOrEmpty(s.Feedback))
            {
                TempData["Message"] = "请在最下面的文本框内输入相应文字";
            }
            else
            {
                s.FullyFilled = true;
                TempData["Message"] = "您已完成信息核对工作，人力资源部将会处理您的申请";
            }
            
            s.FEmployeeID = emp.FEmployeeID;
            if (s.SerialNO == 0)
            {
                ent.TInfoSummaries.Add(s);
            }
            else
            {
                TInfoSummary s2 = ent.TInfoSummaries.Find(s.SerialNO);
                s2.Clone(s);
            }
            ent.SaveChanges();
            return Redirect(UrlHelper.GenerateUrl(null, "Index", "Home", null, null, "basicInfo", new RouteValueDictionary(), Url.RouteCollection, Url.RequestContext, false));
        }
    }
}
